﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows;
using System.Windows.Forms;

namespace ImageProccessingPractice
{
  
    class FileBrowsing
    {
        /// <summary>
        /// File Browsing engine, containing directories and file names 
        /// </summary>
        String newPath = " ";
        String[] directories;
        String[] files;
        Stack<String> previousPaths = new Stack<String>();
        FileBRowser fbr;
        /// <summary>
        /// Constructor 
        /// </summary>
        /// <param name="fbr">File Broweser GUI pointer</param>
        public FileBrowsing(FileBRowser fbr)
        {
            directories = Environment.GetLogicalDrives();
            files = new String[] { "", "" };
            this.fbr = fbr;
        }

        /// <summary>
        /// Another Constructor but with a path to travel to
        /// </summary>
        /// <param name="path">File/Directory path</param>
        public FileBrowsing(String path)
        {
            gotonewDest(path);
        }

        /// <summary>
        /// traverse to the new destination
        /// </summary>
        /// <param name="path">new destination path</param>
        public void gotonewDest(String path)
        {
            try
            {
                if (!path.Equals(newPath))
                {
                    if(!newPath.Equals(" "))
                        previousPaths.Push(newPath);
                    newPath = path;                   
                }
                directories = Directory.GetDirectories(path);
                files = Directory.GetFiles(path);
            }
            catch (Exception e)
            {
                newPath = previousPaths.Pop();
                fbr.Alert(e.Message);
            }
        }

      
       /// <summary>
       /// go back one folder from the previous folders stack. 
       /// </summary>
        
        public void goBack()
        {

            if (previousPaths.Any())
            {
                newPath = previousPaths.Pop();
                directories = Directory.GetDirectories(newPath);
                files = Directory.GetFiles(newPath);
            }
            else
            {
                directories = Environment.GetLogicalDrives();
                files = new String[] { "", "" };
                newPath = " ";
            }
        }

        /// <summary>
        ///  get current file list
        /// </summary>
        public String[] gFiles
        {
            get
            {
                return files;
            }
        }

        /// <summary>
        /// get current folder list
        /// </summary>
        public String[] gDirs
        {
            get 
            {
                return directories;
            }
        }

        
    }
}
